package com.yugao.fintech.framework.mybatismate.config;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.yugao.fintech.framework.desensitization.fieldbind.FieldBindHandler;
import com.yugao.fintech.framework.core.interceptor.MybatisFieldBindCustomInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;


@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisInterceptorCustomConfig {

    @Resource
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Resource
    private FieldBindHandler fieldBindHandler;

    /**
     * 可解决 spring boot 在使用 pagehelper-spring-boot-starter 后 ，自定义拦截器失效的问题。
     */
    @PostConstruct
    public void addMySqlInterceptor() {
        MybatisFieldBindCustomInterceptor fieldBindCustomInterceptor = new MybatisFieldBindCustomInterceptor(fieldBindHandler);
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(fieldBindCustomInterceptor);
        }
    }
}